package thuyan.dtvt.bkhn;

import java.util.Random;

import android.R.bool;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Typeface;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.webkit.WebChromeClient.CustomViewCallback;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.Toast;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;

public class Playgame extends Activity {
	private final static int MAX = 20;
	private TextView[] listTextViews = new TextView[MAX];

	int y = 0;
	private TextView tvQuestion;
	private SimpleCursorAdapter adapter;
	private Cursor cursor;
	TableRow tablerow;
	private String str;
	private int id;
	private LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
			LayoutParams.WRAP_CONTENT);
	private Button btA, btF, btR, btQ, btW, btE, btT, btY, btU, btI, btO, btP,
			btS, btD, btG, btH, btJ, btK, btL, btZ, btX, btC, btV, btB, btN,
			btM;

	private WordGuessHelper datahelper;
	final Random random = new Random();

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_playgame);

		tablerow = (TableRow) findViewById(R.id.tbRow);

		for (int i = 0; i < MAX; i++) {
			listTextViews[i] = new TextView(this);
		}
		tvQuestion = (TextView) findViewById(R.id.tvQuestion);
		// khoi tao du lieu
		datahelper = new WordGuessHelper(Playgame.this);
		// them du lieu
		
		/* datahelper.insertDB("Nghĩa tiếng việt là con chim", "bird");
		 datahelper.insertDB("Nghĩa tiếng việt là con cá sấu", "alligator");
		 datahelper.insertDB("Nghĩa tiếng việt là con chuột", "mouse");
		 datahelper.insertDB("Nghĩa tiếng việt là con rắn", "snake");
		 datahelper.insertDB("Nghĩa tiếng việt là con bướm", "buttafly");
		 datahelper.insertDB("Nghĩa tiếng việt là con trâu", "buffalo");
		 datahelper.insertDB("Nghĩa tiếng việt là con bò", "cow");
		 datahelper.insertDB("Nghĩa tiếng việt là con thỏ", "rabbit");
		 datahelper.insertDB("Nghĩa tiếng việt là con dê", "goat");
		 datahelper.insertDB("Nghĩa tiếng việt là con chuồn chuồn", "fragonfly"); 
		 datahelper.insertDB("Nghĩa tiếng việt là con khỉ", "monkey"); 
		 datahelper.insertDB("Nghĩa tiếng việt là con trăn", "boa");
		 */

		// thuc hien viec lay du lieu ra tu bang
		cursor = datahelper.selectAll();
		//cursor = datahelper.delete();
		startManagingCursor(cursor);
		cursor.moveToPosition(random.nextInt(5));
		id = cursor.getCount();
		Log.d("TAG", "total: " + id);
		setData();

		btA = (Button) findViewById(R.id.btA);

		btA.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub

				btA.setEnabled(false);
				String ch = btA.getText().toString();
				// btA.setBackgroundResource(R.drawable.bg_correct);
				char c = ch.charAt(0);
				displaychar(c);

			}
		});
		btF = (Button) findViewById(R.id.btF);

		btF.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				// btF.setBackgroundResource(R.drawable.bg_correct);

				btF.setEnabled(false);
				String ch = btF.getText().toString();
				char c = ch.charAt(0);
				displaychar(c);

			}
		});
		btR = (Button) findViewById(R.id.btR);

		btR.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// btR.setBackgroundResource(R.drawable.bg_correct);

				btR.setEnabled(false);
				// TODO Auto-generated method stub
				String ch = btR.getText().toString();
				char c = ch.charAt(0);
				displaychar(c);

			}
		});
		btQ = (Button) findViewById(R.id.btQ);
		btQ.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				//btQ.setBackgroundResource(R.drawable.bg_correct);
				btQ.setEnabled(false);
				String ch = btQ.getText().toString();
				char c = ch.charAt(0);
				displaychar(c);

			}
		});
		btW = (Button) findViewById(R.id.btW);
		btW.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				//btW.setBackgroundResource(R.drawable.bg_correct);
				btW.setEnabled(false);
				String ch = btW.getText().toString();
				char c = ch.charAt(0);
				displaychar(c);

			}
		});
		btE = (Button) findViewById(R.id.btE);

		btE.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				btE.setEnabled(false);
				//btE.setBackgroundResource(R.drawable.bg_correct);
				String ch = btE.getText().toString();
				char c = ch.charAt(0);
				displaychar(c);

			}
		});
		btT = (Button) findViewById(R.id.btT);
		btT.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				btT.setEnabled(false);
				//btT.setBackgroundResource(R.drawable.bg_correct);
				String ch = btT.getText().toString();
				char c = ch.charAt(0);
				displaychar(c);

			}
		});
		btY = (Button) findViewById(R.id.btY);
		btY.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				btY.setEnabled(false);
				//btY.setBackgroundResource(R.drawable.bg_correct);
				String ch = btY.getText().toString();
				char c = ch.charAt(0);
				displaychar(c);

			}
		});
		btU = (Button) findViewById(R.id.btU);
		btU.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				btU.setEnabled(false);
				//btU.setBackgroundResource(R.drawable.bg_correct);
				String ch = btU.getText().toString();
				char c = ch.charAt(0);
				displaychar(c);

			}
		});
		btI = (Button) findViewById(R.id.btI);
		btI.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				btI.setEnabled(false);
				//btI.setBackgroundResource(R.drawable.bg_correct);
				String ch = btI.getText().toString();
				char c = ch.charAt(0);
				displaychar(c);

			}
		});
		btO = (Button) findViewById(R.id.btO);
		btO.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				btO.setEnabled(false);
				//btO.setBackgroundResource(R.drawable.bg_correct);
				String ch = btO.getText().toString();
				char c = ch.charAt(0);
				displaychar(c);

			}
		});
		btP = (Button) findViewById(R.id.btP);
		btP.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				btP.setEnabled(false);
				//btP.setBackgroundResource(R.drawable.bg_correct);
				String ch = btP.getText().toString();
				char c = ch.charAt(0);
				displaychar(c);

			}
		});
		btS = (Button) findViewById(R.id.btS);
		btS.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				btS.setEnabled(false);
				//btS.setBackgroundResource(R.drawable.bg_correct);
				String ch = btS.getText().toString();
				char c = ch.charAt(0);
				displaychar(c);

			}
		});
		btD = (Button) findViewById(R.id.btD);
		btD.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				btD.setEnabled(false);
				//btD.setBackgroundResource(R.drawable.bg_correct);
				String ch = btD.getText().toString();
				char c = ch.charAt(0);
				displaychar(c);

			}
		});

		btG = (Button) findViewById(R.id.btG);
		btG.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				btG.setEnabled(false);
				//btG.setBackgroundResource(R.drawable.bg_correct);
				String ch = btG.getText().toString();
				char c = ch.charAt(0);
				displaychar(c);

			}
		});
		btH = (Button) findViewById(R.id.btH);
		btH.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				btH.setEnabled(false);
				//btH.setBackgroundResource(R.drawable.bg_correct);
				String ch = btH.getText().toString();
				char c = ch.charAt(0);
				displaychar(c);

			}
		});
		btJ = (Button) findViewById(R.id.btJ);
		btJ.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				btJ.setEnabled(false);
				//btJ.setBackgroundResource(R.drawable.bg_correct);
				String ch = btJ.getText().toString();
				char c = ch.charAt(0);
				displaychar(c);

			}
		});
		btK = (Button) findViewById(R.id.btK);
		btK.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				btK.setEnabled(false);
				//btK.setBackgroundResource(R.drawable.bg_correct);
				String ch = btK.getText().toString();
				char c = ch.charAt(0);
				displaychar(c);

			}
		});
		btL = (Button) findViewById(R.id.btL);
		btL.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				btL.setEnabled(false);
				//btL.setBackgroundResource(R.drawable.bg_correct);
				String ch = btL.getText().toString();
				char c = ch.charAt(0);
				displaychar(c);

			}
		});
		btZ = (Button) findViewById(R.id.btZ);
		btZ.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				btZ.setEnabled(false);
				//btZ.setBackgroundResource(R.drawable.bg_correct);
				String ch = btZ.getText().toString();
				char c = ch.charAt(0);
				displaychar(c);

			}
		});
		btX = (Button) findViewById(R.id.btX);
		btX.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				btX.setEnabled(false);
				//btX.setBackgroundResource(R.drawable.bg_correct);
				String ch = btX.getText().toString();
				char c = ch.charAt(0);
				displaychar(c);

			}
		});
		btC = (Button) findViewById(R.id.btC);
		btC.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				btC.setEnabled(false);
				//btC.setBackgroundResource(R.drawable.bg_correct);
				String ch = btC.getText().toString();
				char c = ch.charAt(0);
				displaychar(c);

			}
		});
		btV = (Button) findViewById(R.id.btV);
		btV.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				btV.setEnabled(false);
				//btV.setBackgroundResource(R.drawable.bg_correct);
				String ch = btV.getText().toString();
				char c = ch.charAt(0);
				displaychar(c);

			}
		});
		btB = (Button) findViewById(R.id.btB);
		btB.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				btB.setEnabled(false);
				//btB.setBackgroundResource(R.drawable.bg_correct);
				String ch = btB.getText().toString();
				char c = ch.charAt(0);
				displaychar(c);

			}
		});
		btN = (Button) findViewById(R.id.btN);
		btN.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				btN.setEnabled(false);
				//btN.setBackgroundResource(R.drawable.bg_correct);
				String ch = btN.getText().toString();
				char c = ch.charAt(0);
				displaychar(c);

			}
		});
		btM = (Button) findViewById(R.id.btM);
		btM.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				btM.setEnabled(false);
				//btM.setBackgroundResource(R.drawable.bg_correct);
				String ch = btM.getText().toString();
				char c = ch.charAt(0);
				displaychar(c);

			}
		});
	}

	// lay du lieu tu bang ra
	public void setData() {
		if (cursor != null) {
			// cursor.moveToNext();
			// lay duoc thanh cong
			// lay cau hoi ra text view
			tvQuestion.setText(cursor.getString(cursor
					.getColumnIndexOrThrow("question")));
			str = cursor.getString(cursor.getColumnIndexOrThrow("answer"));
			Log.d("Test2", str);
			displayInterface();
		}
	}

	// hien thi o trong
	public void displayInterface() {
		int length = str.length();
		tablerow.removeAllViews();
		for (int i = 0; i < length; i++) {
			// set layout cho textview
			// TextView tv = new TextView(Playgame.this);
			listTextViews[i].setLayoutParams(params);
			listTextViews[i].setBackgroundResource(R.drawable.border);
			listTextViews[i].setText("");
			listTextViews[i].setWidth(1);
			listTextViews[i].setId(i);
			// them text view vao layout
			tablerow.addView(listTextViews[i]);
		}
	}

	// hien thi chu cai len o trong va check xem da dien day cac o trong
	public void displaychar(char c) {
		int length = str.length();
		boolean isCheck = false;

		for (int i = 0; i < length; i++) {

			if (check(c, i)) {
				++y;
				listTextViews[i].setLayoutParams(params);
				listTextViews[i].setText(String.valueOf(c));
				listTextViews[i].setPadding(34, 3, 3, 3);
				listTextViews[i].setTypeface(Typeface.defaultFromStyle(1));
				listTextViews[i].setTextSize(16.0F);
				// listTextViews[i].setBackgroundColor(-16711936);
				listTextViews[i].setTextColor(-16711936);
				// listTextViews[i].setTextSize(16.0F);

				isCheck = true;
				Log.d("TEST", "position: " + i + y);
				if (y == length) {

					SoundPool sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);

					int iTmp = sp.load(Playgame.this, R.raw.doan_dung_mot_doi_tuong, 1); // in 2nd param u have to pass your desire ringtone

					sp.play(iTmp, 1, 1, 0, 0, 1);

					MediaPlayer mPlayer = MediaPlayer.create(Playgame.this, R.raw.doan_dung_mot_doi_tuong); // in 2nd param u have to pass your desire ringtone
					//mPlayer.prepare();
					mPlayer.start();
					// cursor.moveToNext(); setData();
					y = 0;
					Log.d("TEST", "string: " + str);

					// showNotification();
					AlertDialog.Builder builder = new AlertDialog.Builder(
							Playgame.this);
					builder.setIcon(R.drawable.successful);
					builder.setTitle("Thong bao");
					builder.setMessage("Chúc mừng, bạn đã trả lời chính xác!!!!");
					builder.setPositiveButton("Tiếp tục",
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									// TODO Auto-generated method stub

									btF.setEnabled(true);
									btA.setEnabled(true);
									btR.setEnabled(true);
									btQ.setEnabled(true);
									btW.setEnabled(true);
									btE.setEnabled(true);
									btT.setEnabled(true);
									btY.setEnabled(true);
									btU.setEnabled(true);
									btI.setEnabled(true);
									btO.setEnabled(true);
									btP.setEnabled(true);
									btS.setEnabled(true);
									btD.setEnabled(true);
									btG.setEnabled(true);
									btH.setEnabled(true);
									btJ.setEnabled(true);
									btK.setEnabled(true);
									btL.setEnabled(true);
									btZ.setEnabled(true);
									btX.setEnabled(true);
									btC.setEnabled(true);
									btV.setEnabled(true);
									btB.setEnabled(true);
									btN.setEnabled(true);
									btM.setEnabled(true);
									
									if (cursor.getPosition() == (cursor.getCount()-1)) {
										cursor.moveToFirst();
										Log.d("TEST", "" + cursor.getPosition());
									} else
										cursor.moveToNext();
									// Log.d("TEST", ""+cursor.getPosition());
									// cursor.moveToNext();
									// Log.d("TEST", ""+cursor.getCount());
									setData();

								}
							});
					builder.setNegativeButton("Quay về",
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									// TODO Auto-generated method stub
									Intent myIntent = new Intent(Playgame.this,
											Play.class);
									startActivity(myIntent);
								}
							});
					builder.show();
				}

			}

		}

		if (!isCheck) {
			Toast.makeText(this, "Sai roi!!!", Toast.LENGTH_SHORT).show();
		}
	}

	// ham kiem tra chu cai co trong dap an hay khong va hien thi len textview
	public boolean check(char c, int i) {
		Log.d("TEST", "check char; " + c + " - po: " + str.charAt(i));
		boolean check = false;
		if (Character.toUpperCase(str.charAt(i)) == c) {
			return true;
		}
		return check;

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.playgame, menu);
		return true;
	}
	

}
